package com.demo.friend.service;


import com.demo.friend.dao.FriendDao;
import com.demo.friend.dao.NoFriendDao;
import com.demo.friend.pojo.Friend;
import com.demo.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2020/7/20.
 */
@Service
@Transactional
public class FriendService {

    @Autowired
    private FriendDao friendDao;

    @Autowired
    private NoFriendDao noFriendDao;

    public int add(String userId, String friendid) {
         //先判断userid到friend是否有数据，有就是重复添加数据
        Friend friend = friendDao.findByUseridAndFriendid(userId, friendid);
        if (friend != null){
            return 0;
        }
        //直接添加好友，让好友表中的userid到friendid方向的type为0
        friend = new Friend();
        friend.setUserid(userId);
        friend.setFriendid(friendid);
        friend.setIslike("0");
        friendDao.save(friend);
        //判断双方互为好友
        if (friendDao.findByUseridAndFriendid(friendid,userId) != null){
            friendDao.updateIslike("1",userId,friendid);
            friendDao.updateIslike("1",friendid,userId);
        }
        return 1;
    }

    public int addNofriend(String userId, String friendid) {
        //判断是否为好友
        NoFriend noFriend = noFriendDao.findByUseridAndFriendid(userId,friendid);
        if (noFriend != null){
            return 0;
        }
        noFriend = new NoFriend();
        noFriend.setFriendid(userId);
        noFriend.setFriendid(friendid);
        noFriendDao.save(noFriend);
        return  1;
    }

    public void deleteFriend(String userId, String friendid) {
        //删除好友表中userid到friedid这条数据
        friendDao.deleteFriend(userId,friendid);
        //更新friendid到userid的lslike为0
        friendDao.updateIslike("0",friendid,userId);
        //非好友中添加数据
        NoFriend noFriend = new NoFriend();
        noFriend.setFriendid(friendid);
        noFriend.setUserid(userId);
        noFriendDao.save(noFriend);
    }
}
